<template>
  <div class="dashboard">
    <!-- 统计卡片 -->
    <div class="stats-grid">
      <div class="stat-card" v-for="stat in stats" :key="stat.title">
        <div class="stat-icon" :style="{ background: stat.color }">
          <span v-html="stat.icon"></span>
        </div>
        <div class="stat-content">
          <h3 class="stat-value">{{ stat.value }}</h3>
          <p class="stat-title">{{ stat.title }}</p>
          <span class="stat-trend" :class="{ positive: stat.trend > 0, negative: stat.trend < 0 }">
            {{ stat.trend > 0 ? '+' : '' }}{{ stat.trend }}%
          </span>
        </div>
      </div>
    </div>

    <!-- 图表区域 -->
    <div class="charts-section">
      <div class="chart-container">
        <div class="chart-header">
          <h3>访问量趋势</h3>
          <div class="chart-actions">
            <button class="btn btn-secondary">今日</button>
            <button class="btn btn-secondary">本周</button>
            <button class="btn btn-primary">本月</button>
          </div>
        </div>
        <div class="chart-placeholder">
          <svg class="chart-svg" viewBox="0 0 400 200">
            <polyline
              points="20,180 60,120 100,140 140,80 180,100 220,60 260,90 300,40 340,70 380,30"
              fill="none"
              stroke="#1890ff"
              stroke-width="3"
            />
            <circle cx="380" cy="30" r="4" fill="#1890ff"/>
          </svg>
          <p class="chart-label">访问量统计图表</p>
        </div>
      </div>

      <div class="chart-container">
        <div class="chart-header">
          <h3>热门页面</h3>
        </div>
        <div class="ranking-list">
          <div class="ranking-item" v-for="(item, index) in rankings" :key="index">
            <span class="ranking-index" :class="{ top: index < 3 }">{{ index + 1 }}</span>
            <span class="ranking-title">{{ item.title }}</span>
            <span class="ranking-value">{{ item.value }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 最近活动 -->
    <div class="activity-section">
      <div class="section-header">
        <h3>最近活动</h3>
        <router-link to="/logs" class="btn btn-secondary">查看全部</router-link>
      </div>
      <div class="activity-list">
        <div class="activity-item" v-for="activity in activities" :key="activity.id">
          <div class="activity-avatar">
            <span v-html="activity.icon"></span>
          </div>
          <div class="activity-content">
            <p class="activity-text">{{ activity.text }}</p>
            <span class="activity-time">{{ activity.time }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const stats = ref([
  {
    title: '总用户数',
    value: '12,680',
    trend: 12.5,
    color: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
    icon: '<svg viewBox="0 0 24 24"><path d="M16 4c0-1.11.89-2 2-2s2 .89 2 2-.89 2-2 2-2-.89-2-2M4 18v-1c0-1.28.84-2.5 2.14-2.74L9.5 14.5c.28-.1.58-.14.9-.14H17v4H4Z"/></svg>'
  },
  {
    title: '今日订单',
    value: '1,234',
    trend: 8.2,
    color: 'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)',
    icon: '<svg viewBox="0 0 24 24"><path d="M7 4V2C7 1.45 7.45 1 8 1H16C16.55 1 17 1.45 17 2V4H20C20.55 4 21 4.45 21 5S20.55 6 20 6H19V19C19 20.1 18.1 21 17 21H7C5.9 21 5 20.1 5 19V6H4C3.45 6 3 5.55 3 5S3.45 4 4 4H7Z"/></svg>'
  },
  {
    title: '月度收入',
    value: '¥89,234',
    trend: -2.1,
    color: 'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)',
    icon: '<svg viewBox="0 0 24 24"><path d="M12 2C13.1 2 14 2.9 14 4C14 5.1 13.1 6 12 6C10.9 6 10 5.1 10 4C10 2.9 10.9 2 12 2ZM21 9V7L15 1L13.5 2.5L16.17 5.17L10.5 10.84L6.5 8.84L4 11.34L5.5 12.84L8.34 10L12.34 12L18.67 5.67L21 8V9Z"/></svg>'
  },
  {
    title: '活跃用户',
    value: '8,456',
    trend: 15.3,
    color: 'linear-gradient(135deg, #fa709a 0%, #fee140 100%)',
    icon: '<svg viewBox="0 0 24 24"><path d="M12 12C14.21 12 16 10.21 16 8S14.21 4 12 4 8 5.79 8 8 9.79 12 12 12M12 14C9.33 14 4 15.34 4 18V20H20V18C20 15.34 14.67 14 12 14Z"/></svg>'
  }
])

const rankings = ref([
  { title: '首页', value: '2,345' },
  { title: '商品列表', value: '1,890' },
  { title: '用户中心', value: '1,456' },
  { title: '订单管理', value: '892' },
  { title: '设置页面', value: '567' }
])

const activities = ref([
  {
    id: 1,
    text: '用户 张三 下了一个新订单',
    time: '5分钟前',
    icon: '<svg viewBox="0 0 24 24"><path d="M12 2C13.1 2 14 2.9 14 4C14 5.1 13.1 6 12 6C10.9 6 10 5.1 10 4C10 2.9 10.9 2 12 2Z"/></svg>'
  },
  {
    id: 2,
    text: '系统备份已完成',
    time: '1小时前',
    icon: '<svg viewBox="0 0 24 24"><path d="M12,15.5A3.5,3.5 0 0,1 8.5,12A3.5,3.5 0 0,1 12,8.5A3.5,3.5 0 0,1 15.5,12A3.5,3.5 0 0,1 12,15.5Z"/></svg>'
  },
  {
    id: 3,
    text: '新用户 李四 注册成功',
    time: '2小时前',
    icon: '<svg viewBox="0 0 24 24"><path d="M16 4c0-1.11.89-2 2-2s2 .89 2 2-.89 2-2 2-2-.89-2-2Z"/></svg>'
  }
])
</script>

<style scoped>
.dashboard {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0;
}

/* 统计卡片 */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 24px;
  margin-bottom: 32px;
}

.stat-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  border: 1px solid #f0f0f0;
  transition: all 0.3s ease;
}

.stat-card:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.stat-icon {
  width: 64px;
  height: 64px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.stat-icon svg {
  width: 32px;
  height: 32px;
  fill: white;
}

.stat-content {
  flex: 1;
}

.stat-value {
  font-size: 28px;
  font-weight: 600;
  color: #262626;
  margin: 0 0 4px 0;
}

.stat-title {
  font-size: 14px;
  color: #8c8c8c;
  margin: 0 0 8px 0;
}

.stat-trend {
  font-size: 12px;
  font-weight: 500;
  padding: 2px 6px;
  border-radius: 4px;
}

.stat-trend.positive {
  color: #52c41a;
  background: #f6ffed;
}

.stat-trend.negative {
  color: #ff4d4f;
  background: #fff2f0;
}

/* 图表区域 */
.charts-section {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 24px;
  margin-bottom: 32px;
}

.chart-container {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  border: 1px solid #f0f0f0;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.chart-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
  color: #262626;
}

.chart-actions {
  display: flex;
  gap: 8px;
}

.chart-placeholder {
  text-align: center;
  padding: 20px;
}

.chart-svg {
  width: 100%;
  height: 200px;
  margin-bottom: 16px;
}

.chart-label {
  color: #8c8c8c;
  font-size: 14px;
  margin: 0;
}

/* 排行榜 */
.ranking-list {
  space-y: 12px;
}

.ranking-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.ranking-item:last-child {
  border-bottom: none;
}

.ranking-index {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #f5f5f5;
  color: #8c8c8c;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 500;
  flex-shrink: 0;
}

.ranking-index.top {
  background: #1890ff;
  color: white;
}

.ranking-title {
  flex: 1;
  font-size: 14px;
  color: #262626;
}

.ranking-value {
  font-size: 14px;
  font-weight: 500;
  color: #595959;
}

/* 活动区域 */
.activity-section {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  border: 1px solid #f0f0f0;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.section-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
  color: #262626;
}

.activity-list {
  space-y: 16px;
}

.activity-item {
  display: flex;
  gap: 12px;
  padding: 12px 0;
}

.activity-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #f0f2f5;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.activity-avatar svg {
  width: 20px;
  height: 20px;
  fill: #595959;
}

.activity-content {
  flex: 1;
}

.activity-text {
  margin: 0 0 4px 0;
  font-size: 14px;
  color: #262626;
}

.activity-time {
  font-size: 12px;
  color: #8c8c8c;
}

@media (max-width: 768px) {
  .charts-section {
    grid-template-columns: 1fr;
  }
  
  .stats-grid {
    grid-template-columns: 1fr;
  }
}
</style> 